package com.pepository.dao;

import com.aop.Detach;
import com.model.FrontUser;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.time.LocalDateTime;

public interface FrontUserRepository extends JpaRepository<FrontUser, Long> {

    @Detach
    @Override
    <S extends FrontUser> S save(S entity);

    @Query( value = "SELECT COUNT(id) from front_user where username = :username",
            nativeQuery = true)
    int hasByUsername(@Param("username") String username);

    @Query( value = "SELECT COUNT(id) from front_user where username = :username and password = :password",
            nativeQuery = true)
    int hasByUsername(@Param("username")String username, @Param("password")String password);

    @Modifying
    @Query( value = "UPDATE front_user SET login_time = :now where username = :username",
            nativeQuery = true)
    void updateLoginTime(@Param("username") String username, @Param("now") LocalDateTime now);

    FrontUser findByUsername(String username);
}
